<?php
namespace app\admin\model;
use think\Model;
use think\Db;

class Admin extends Model{

	public function login($data){
		$captcha = new \think\captcha\Captcha();
        if (!$captcha->check($data['code'])) {
            return 4;
        }
		$user = self::where('username',$data['username'])->find();
		if($user){
			if($user['password'] == md5($data['password'])){
				session('ADMIN_USERNAME',$user['username']);
				session('ADMIN_ID',$user['id']);
				return 3; //信息正确
			}else{
				return 2; //密码错误
			}
		}else{
			return 1; //用户不存在
		}
	}

    /**
     * 管理员列表
     */
    public function lst(){
        //return $data = db('admin')->paginate();
        return $data = db('admin')->select();
    }
	public function getUserById($adminID){
        $user = db('admin')->where(['id' => $adminID])->find();
        return $user;
    }
    public function getUserByUsername($username){
        $user = db('admin')->where(['username' => $username])->find();
        return $user;
    }
    public function insertAdmin($data){
        return db('admin')->insert($data);
    }

}
